/*
 * binarySearch.cpp
 *
 *  Created on: Sep 6, 2012
 *      Author: jeffrey
 */
#include<iostream>
using namespace std;
class BinarySearch {
public:
	static int search(int array[], int n, int key) {
//		return binarySearch(array, 0, n, key);
		return binarySearchIterative(array,n,key);
	}
	static int binarySearch(int array[], int s, int e, int key) {
		if (s > e)
			return -1;
		int mid = (s + e) / 2;
		if (array[mid] == key)
			return mid;
		else if (array[mid] > key)
			return binarySearch(array, s, mid - 1, key);
		else
			return binarySearch(array, mid + 1, e, key);

	}
	static int binarySearchIterative(int array[], int n, int key) {
		int s = 0, e = n - 1;
		while (s <= e) {
			int mid = (s + e) / 2;
			if (array[mid] == key)
				return mid;
			else if (array[mid] > key)
				e = mid - 1;
			else
				s = mid + 1;
		}
		return -1;
	}
};
//int main(){
//	int array[8] = { 2,4,5,6,7,10,12,13 };
//	cout<<BinarySearch::search(array,8,13)<<endl;
//}

